package controllers

import (
	//"fmt"
	"regexp"
	"strings"
)

func ConvertFromWikiToHtml(wiki string) string {
	//add tags into this slice
	wikiTag := []string{"[h1]:<h1>", "[/h1]:</h1>", "[br]:<br/>", "[p]:<p>", "[/p]:</p>"}
	wikiTag = append(wikiTag, "[hr]:<hr>", "[h2]:<h2>", "[/h2]:</h2>", "[h3]:<h3>", "[/h3]:</h3>")
	wikiTag = append(wikiTag, "[h4]:<h4>", "[/h4]:</h4>", "[h5]:<h5>", "[/h5]:</h5>", "[h6]:<h6>", "[/h6]:</h6>")
	wikiTag = append(wikiTag, "[lead]:<p class='lead'>", "[/lead]:</p>", "[strong]:<strong>", "[/strong]:</strong>")
	wikiTag = append(wikiTag, "[em]:<em>", "[/em]:</em>", "[left]:<p class='text-left'>", "[/left]:</p>")
	wikiTag = append(wikiTag, `[center]:<p class="text-center">`, "[/center]:</p>", "[right]:<p class='text-right'>", "[/right]:</p>")
	wikiTag = append(wikiTag, "[ul]:<ul>", "[/ul]:</ul>", "[ol]:<ol>", "[/ol]:</ol>", "[li]:<li>", "[/li]:</li>")
	wikiTag = append(wikiTag, "[flash]:<embed ", "[/flash]:</embed>")
	for _, tag := range wikiTag {
		tt := strings.Split(tag, ":")
		wiki = strings.Replace(wiki, tt[0], tt[1], -1)
	}

	//处理wiki标签
	regx := "\\[wiki\\].*?\\[\\/wiki\\]"
	re, _ := regexp.Compile(regx)

	for re.MatchString(wiki) {
		addr := re.FindString(wiki)
		//fmt.Println(addr)
		//fmt.Println(addr[6 : len(addr)-8])

		addrnew := `<a href="/?wiki=` + addr[6:len(addr)-7] + `"><span class="label label-success">` + addr[6:len(addr)-7] + `</span></a>`
		//fmt.Println(addrnew)
		wiki = strings.Replace(wiki, addr, addrnew, -1)
	}

	//处理link标签
	regx = "\\[link\\].*?\\[\\/link\\]"
	re, _ = regexp.Compile(regx)

	for re.MatchString(wiki) {
		addr := re.FindString(wiki)
		addrwithouttag := addr[6 : len(addr)-7]
		t := strings.Split(addrwithouttag, "::")
		if len(t) != 2 {
			break
		} else {
			addrnew := `<a href='` + t[1] + `' target='_blank'><span class="label label-important">` + t[0] + `</span></a>`
			wiki = strings.Replace(wiki, addr, addrnew, -1)
		}
	}

	//处理img标签
	regx = "\\[imgs\\].*?\\[\\/imgs\\]"
	re, _ = regexp.Compile(regx)
	for re.MatchString(wiki) {
		addr := re.FindString(wiki)
		addrnew := `<img src="` + addr[6:len(addr)-7] + `" />`
		wiki = strings.Replace(wiki, addr, addrnew, -1)
	}

	//处理tabl标签
	regx = "\\[tabl\\].*?\\[\\/tabl\\]"
	re, _ = regexp.Compile(regx)
	for re.MatchString(wiki) {
		addr := re.FindString(wiki)
		addrwithouttag := addr[6 : len(addr)-7]
		addrnew := `<table class="table table-bordered">`
		rows := strings.Split(addrwithouttag, "<br/>")
		for _, row := range rows {
			if row == "" {
				continue
			}
			//fmt.Printf(row + "===")
			addrnew = addrnew + `<tr>`
			cols := strings.Split(row, "||")
			for _, col := range cols {
				if col == "" {
					continue
				}
				//fmt.Printf(col + "...")
				addrnew = addrnew + `<td>` + col + `</td>`
			}
			addrnew = addrnew + `</tr>`
		}
		addrnew = addrnew + `</table>`

		wiki = strings.Replace(wiki, addr, addrnew, -1)
	}

	return wiki
}

func ReplaceHtml(html string) string {
	html = strings.Replace(html, "<", `&lt;`, -1)
	html = strings.Replace(html, ">", `&gt;`, -1)
	return html
}

func GetTitle(content string) string {
	title := strings.Split(content, "\r\n")
	if len(title) > 0 {
		return title[0]
	}
	return ""

}

func GetContent(content string) string {
	list := strings.Split(content, "\r\n")
	if len(list) <= 1 {
		return ""
	}
	cc := ""
	list2 := list[1:]
	for _, line := range list2 {
		cc = cc + line + "[br]"
	}
	cc = cc[:len(cc)-4]
	return cc
}
